<?php
// https://mp.weixin.qq.com/s/yLc7-CZdti8gEMGWhd0JTg
class Solution {
    /**
     * 快慢指针
     * @param ListNode $head
     * @return Boolean
     */
    function hasCycle($head) {
        if ($head == null || $head->next == null) {
            return false;
        }

        $slow = $fast = $head;

        // 这里为什么不用判断 $fast->next->next == null ， 是因为$fast->next不为null $fast->next->next一定有值或者为null
        while ($fast != null && $fast->next != null) {
            $fast = $fast->next->next;
            $slow = $slow->next;
            if ($fast === $slow) {
                return true;
            }
        }
        return false;
    }
}

class ListNode {
    public $val = 0;
     public $next = null;
     function __construct($val) { $this->val = $val; }
 }